package com.yiaohuer.controller;

import com.yiaohuer.DTO.CampusDTO;
import com.yiaohuer.VO.CampusVO;
import com.yiaohuer.entity.Campus;
import com.yiaohuer.entity.PageResult;
import com.yiaohuer.entity.Result;
import com.yiaohuer.service.CampusService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

@RestController
@RequestMapping("/campus/list")

@Slf4j
public class CampusController {
    @Resource
    private CampusService campusService;

    @PostMapping("/add")
    public Result<String> setCampus(@RequestBody CampusDTO campusDTO){

        try {
            log.info("com.yiaohuer.campus.add.campusDTO:{}",campusDTO);
            campusService.insert(campusDTO);
            return Result.ok();
        } catch (Exception e) {
            return Result.fail();
        }
    }

    @PostMapping("/update")
    public Result<String> update(@RequestBody CampusDTO campusDTO){
        try {
            log.info("com.yiaohuer.campus.update.campusDTO:{}",campusDTO);
            campusService.update(campusDTO);
            return Result.ok();
        } catch (Exception e) {
            return Result.fail();
        }
    }

    @PostMapping("/delete")
    public Result<String> delete(@RequestBody CampusDTO campusDTO){

        try {
            log.info("com.yiaohuer.campus.delete.campusDTO:{}",campusDTO);
            Integer id = campusDTO.getId();
            boolean result = campusService.deleteById(id);
            return Result.ok(result);
        } catch (Exception e) {
            return Result.fail();
        }
    }
    @PostMapping("/query")
    public Result<PageResult<Campus>> query(@RequestBody CampusDTO campusDTO){
        try {
            log.info("com.yiaohuer.campus.query.campusDTO:{}",campusDTO);

            PageResult<Campus> campusPageResult = campusService.getCampusPage(campusDTO);
            return Result.ok(campusPageResult);

        } catch (Exception e) {
            log.error(e.getMessage(),e);
            return Result.fail();
        }
    }


    @GetMapping("/{id}")
    public Result<Campus> queryById(Integer id){
        try {
            CampusDTO campusDTO = new CampusDTO();
            campusDTO.setId(id);
            Campus campus = campusService.queryById(campusDTO.getId());
            return Result.ok(campus);
        } catch (Exception e) {
            return Result.fail();
        }
    }

}
